package com.ler.yrmall.product.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ler.yrmall.exception.BusinessException;
import com.ler.yrmall.product.entity.BrandEntity;
import com.ler.yrmall.product.entity.CategoryBrandRelationEntity;
import com.ler.yrmall.product.mapper.CategoryBrandRelationMapper;
import com.ler.yrmall.product.service.CategoryBrandRelationService;
import com.ler.yrmall.product.vo.CategoryBrandRelationVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author leron
 */
@Service
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationMapper, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Override
    public List<CategoryBrandRelationVO> listByBrandId(CategoryBrandRelationEntity request) {
        return this.baseMapper.listByBrandId(request);
    }

    @Override
    public void saveRelation(CategoryBrandRelationEntity request) {
        CategoryBrandRelationEntity one = this.getOne(Wrappers.<CategoryBrandRelationEntity>lambdaQuery()
                .eq(CategoryBrandRelationEntity::getCategoryId, request.getCategoryId())
                .eq(CategoryBrandRelationEntity::getBrandId, request.getBrandId()));
        Assert.isNull(one,() -> new BusinessException("已存在相同的关联"));
        this.save(request);
    }

    @Override
    public List<BrandEntity> getBrandByCategoryId(String categoryId) {
        return this.baseMapper.getBrandByCategoryId(categoryId);
    }

}